package com.ldc.common.common;

import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class CommonRecyclerView<T> extends RecyclerView.Adapter<CommonRecyclerViewHolder> {
    private final List<T> dataList;
    private final @LayoutRes Integer layoutId;

    public CommonRecyclerView(int layoutId) {
        this.layoutId = layoutId;
        dataList = new ArrayList<>();
    }


    public void setNewData(List<T> data) {
        dataList.clear();
        if (null != data) {
            dataList.addAll(data);
        }
        notifyDataSetChanged();
    }

    public void addNewData(List<T> data) {
        int size = dataList.size();
        if (null != data) {
            dataList.addAll(data);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CommonRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return CommonRecyclerViewHolder.get(parent, layoutId);

    }

    @Override
    public void onBindViewHolder(@NonNull CommonRecyclerViewHolder holder, int position) {
        if (dataList.isEmpty()) {
            return;
        }
        T data = dataList.get(position);
        if (null == data) {
            return;
        }
        convert2Data(holder, data, position);
    }

    @Override
    public int getItemCount() {
        return dataList.isEmpty() ? 0 : dataList.size();
    }


    protected abstract void convert2Data(@NonNull CommonRecyclerViewHolder holder, T bean, int position);
}



